package com.example.sqlview;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class StudentAdapter extends BaseAdapter {
    private Context context;
    private List<StudentBean> studentList;

    public StudentAdapter(Context context, List<StudentBean> studentList) {
        this.context = context;
        this.studentList = studentList;
    }

    @Override
    public int getCount() {
        return studentList.size();
    }

    @Override
    public Object getItem(int position) {
        return studentList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_student, parent, false);
            holder = new ViewHolder();
            holder.tvName = convertView.findViewById(R.id.tv_name);
            holder.tvNumber = convertView.findViewById(R.id.tv_number);
            holder.tvGender = convertView.findViewById(R.id.tv_gender);
            holder.tvScore = convertView.findViewById(R.id.tv_score);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        StudentBean student = studentList.get(position);
        holder.tvName.setText(student.getName());
        holder.tvNumber.setText(student.getNumber());
        holder.tvGender.setText(student.getGender());
        holder.tvScore.setText(student.getScore());

        return convertView;
    }

    private static class ViewHolder {
        TextView tvName;
        TextView tvNumber;
        TextView tvGender;
        TextView tvScore;
    }
}
